﻿using Avalonia;
using ReactiveUI;
using System;
using System.Reactive.Linq;

public class AvaloniaActivationForViewFetcher : IActivationForViewFetcher
{
    public int GetAffinityForView(Type view)
    {
        // 判断当前视图是否是 Avalonia 的 Window 类型
        return typeof(Avalonia.Controls.Window).IsAssignableFrom(view) ? 10 : 0;
    }

    public IObservable<bool> GetActivationForView(IActivatableView view)
    {
        throw new NotImplementedException();
    }

    public IObservable<bool> GetActivationForView(object view)
    {
        if (view is Avalonia.Controls.Window window)
        {
            // 返回一个 Observable，表示窗口的激活和停用状态
            return Observable.Merge(
                Observable.FromEventPattern<EventHandler, EventArgs>(
                    h => window.Activated += h,
                    h => window.Activated -= h
                ).Select(_ => true),
                Observable.FromEventPattern<EventHandler, EventArgs>(
                    h => window.Deactivated += h,
                    h => window.Deactivated -= h
                ).Select(_ => false)
            );
        }
        return Observable.Empty<bool>();
    }
}